package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.ui.MenuItem;
import java.util.Vector;

import com.sound.ampache.objects.Album;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ListField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.component.ObjectListField;


class AlbumsScreen extends ItemsScreen implements HTTPTransportListener {
	
	private Album[] albums;
	private AlbumsListField albumsList;
	private SongsScreen songsScreen;
	public String screenTitle = "Albums";
	
    Font mainFont = Font.getDefault().derive(Font.PLAIN, 24);
    
	private MenuItem viewSongsMenu = new MenuItem("View Songs",100,10) {
	    public void run() {
	        viewSongs();
	    } 
	 };
     
     private MenuItem addSongsMenu = new MenuItem("Add Songs to Playlist",100,10) {
         public void run() {
             addSongs();
         } 
      };
    
	AlbumsScreen()
	{		
		albumsList = new AlbumsListField();
		ItemsScreen.itemsList = albumsList;
        setTitle( new LabelField( screenTitle , LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH ) );
        albumsList.setEmptyString("No Albums", DrawStyle.HCENTER);
        this.add(itemsList); 
	}
	
	
	public void getAlbums(){
		// this method is called to get all albums,
		// so get the cached albums if they exist
		// and if the user has it checked in Options
		// Also get albums if the user has checked
		// autoupdate and the album catalog size has changed
		albums = DataStore.getCachedAlbums();
		if(albums.length == 0 || albums == null ||
			DataStore.getCacheVal().booleanValue() == false ||
			(DataStore.getCacheVal().booleanValue() == true &&
					DataStore.getUpdateVal().booleanValue() == true &&
					albums.length != ampacheberry.comm.getAlbums())) {
				getAlbums("albums", "");
		} else {
			statusDlg.close();
			albumsList.set(albums);
		}
	}
	
	
	public void getAlbums(String filter){
		getAlbums("albums", filter);
	}
	
	
	public void getAlbums(String method, String filter){
		setTitle(screenTitle);
		albumsList.set(null); 
		statusDlg.show();
	    ampacheberry.comm.setHTTPTransportListener(this);
	    ampacheberry.comm.send(method, filter); 
	}
	
	
    protected void makeMenu(Menu menu, int instance) {
        menu.add(viewSongsMenu);
        menu.add(addSongsMenu);
        menu.add(MenuItem.separator(addSongsMenu.getOrdinal() + 1));
        super.makeMenu(menu,instance);
    }

    
    protected class AlbumsListField extends ObjectListField {
        
        private Bitmap icon = Bitmap.getBitmapResource("img/album.png");  
    	 
        // We are going to take care of drawing the item.
        public void drawListRow(ListField listField, Graphics graphics, 
                int index, int y, int width) {     
                    
            int offsetY = (this.getRowHeight() - icon.getHeight())/2;
            graphics.drawBitmap(1,y + offsetY, icon.getWidth(),
            		icon.getHeight(),icon,0,0);
            graphics.drawText("Album: " + albums[index].name, 
            		icon.getWidth() + 4, y + 1, DrawStyle.ELLIPSIS,
            		width - icon.getWidth() + 2);
            graphics.drawText("Artist: " + albums[index].artist, 
                    icon.getWidth() + 4, y + fontSize + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawText("Tracks: " + albums[index].tracks, 
                    icon.getWidth() + 4, y + (2 * fontSize) + 2,
                    DrawStyle.ELLIPSIS, width - icon.getWidth() + 2);
            graphics.drawLine(0, y + rowHeight - 1, listField.getWidth(),
            		y + rowHeight - 1);
        }
    };
	
    
	public void processResponse(final Vector data) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run()
            {
            	statusDlg.close();
    			albums = new Album[data.size()];
    			data.copyInto(albums);
    			albumsList.set(albums);
    			
    			// cache the albums if the whole catalog was retrieved
    			// and the user has selected to cache the albums
    			if (data.size() == ampacheberry.comm.getAlbums() &&
    				DataStore.getCacheVal().booleanValue() == true) {
	    				DataStore.setCachedAlbums(albums);
    			}
            }
        });
	}
    
	
    protected boolean navigationClick(int status, int time)
    {
        viewSongs();
        return true;
    }

	
    private void viewSongs() {
    	int selected = albumsList.getSelectedIndex();
    	
    	if (null == songsScreen) {
    		songsScreen = new SongsScreen();
    	}
    	songsScreen.screenTitle = "Songs on " + albums[selected].name;
    	songsScreen.setRowHeight();
    	UiApplication.getUiApplication().pushScreen(songsScreen);
        songsScreen.getSongs("album_songs", albums[selected].id);
    }
    
    
    private void addSongs() {
    	int selected = albumsList.getSelectedIndex();
    	ampacheberry.playlistSong.addAlbumToPlaylist(albums[selected]);
    }
    
    
} 
